
#include "configs.h"
#include <stdio.h>        // for printf
#include <stdlib.h>
#include <signal.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <mtd/mtd-user.h>
#include <linux/types.h>

USER_CONFIG_DATA m_UserConfig;
SYS_CONFIG_DATA m_SysConfig;


static pthread_mutex_t userCfg_mut     = PTHREAD_MUTEX_INITIALIZER;

int UserCfg_Save();
int UserCfg_Read();
int UserCfg_Init();
int UserCfg_Exec(unsigned char ExecItem);
int UserCfg_Init()
{
  UserCfg_Read();
  UserCfg_Exec(CONFIG_EXEC_ALL);
  return 0;
}
int UserCfg_Exec(unsigned char ExecItem)
{
  char buff[100];
  if(m_UserConfig.LanType == 2 && (ExecItem == CONFIG_EXEC_ALL || ExecItem == CONFIG_EXEC_IP))
  {
    sprintf(buff,"ifconfig eth0 %d.%d.%d.%d netmask %d.%d.%d.%d",m_UserConfig.LanIP[0],m_UserConfig.LanIP[1],m_UserConfig.LanIP[2],
      m_UserConfig.LanIP[3],m_UserConfig.LanNetmask[0],m_UserConfig.LanNetmask[1],m_UserConfig.LanNetmask[2],m_UserConfig.LanNetmask[3]);
    system(buff);
    sprintf(buff,"route add default gw %d.%d.%d.%d",m_UserConfig.LanGateway[0],m_UserConfig.LanGateway[1],m_UserConfig.LanGateway[2],
      m_UserConfig.LanGateway[3]);
    system(buff);
  }
  return 0;
}
int UserCfg_Save()
{
  int dev_fd;
  int result;
  erase_info_t erase;
  mtd_info_t mtd;
  char *buff;
  unsigned int buffLen;
  dev_fd = open ("/dev/mtd5", O_SYNC | O_RDWR);
  if(dev_fd < 0)
  {
    mLog("open mtd failed!\n");
    close(dev_fd);
    return -1;
  }
  if (ioctl (dev_fd, MEMGETINFO, &mtd) < 0)
  {
    printf("get mtd info failed!\n");
    close(dev_fd);
    return -1;
  }
  mLog("mtd.size=%d,mtd.erasesize=%d,mtd.writesize=%d\n",mtd.size,mtd.erasesize,mtd.writesize);

  erase.start = 0;
  erase.length = (sizeof(m_UserConfig)/mtd.erasesize + 1)*mtd.erasesize;
  result = ioctl (dev_fd, MEMERASE, &erase);
  if (result < 0)
  {
    mLog("erase mtd failed!,errorID:%d\n",result);
    close(dev_fd);
    return 1;
  }
  buffLen =  (sizeof(m_UserConfig)/mtd.writesize + 1)*mtd.writesize;
  buff = malloc(buffLen);
  memcpy(buff,&m_UserConfig,sizeof(m_UserConfig));
  result = write(dev_fd,buff,buffLen);
  free(buff);
  if (result < buffLen)
  {
    mLog("write mtd failed!,errorID:%d\n",result);
    close(dev_fd);
    return 1;
  }
  close(dev_fd);
  return 0;
  /*
  int length;
  FILE* fd;  
  fd = fopen("userConfig.ini","wb+");
  if(fd == 0)
    return 1;
  pthread_mutex_lock(&userCfg_mut);
  length = fwrite(&m_UserConfig,sizeof(m_UserConfig),1,fd);
  pthread_mutex_unlock(&userCfg_mut);
  fclose(fd);
  return 0;
  */
}

int UserCfg_Read()
{
  int dev_fd;
  int len;
  mLog("begin reading config from nand flash..\n");
  dev_fd = open ("/dev/mtd5", O_SYNC | O_RDWR);
  if(dev_fd < 0)
  {
    mLog("open mtd failed!\n");
    close(dev_fd);
    return 1;
  }
  len = read(dev_fd, &m_UserConfig, sizeof(m_UserConfig));
  if(len < sizeof(m_UserConfig))
  {
    mLog("read mtd failed!\n");
    close(dev_fd);
    return 1;
  }
  if(m_UserConfig.configID !=SEPC_CONFIG_ID)
  {
    memset(&m_UserConfig,0,sizeof(m_UserConfig));
    m_UserConfig.configID = SEPC_CONFIG_ID;
  }
  close(dev_fd);
  mLog("finished reading config from nand flash..\n");
  return 0;
	/*
  int length;
  FILE* fd;
  fd = fopen("userConfig.ini","rb+");
  if(fd == 0)
    return 1;
  pthread_mutex_lock(&userCfg_mut);
  length = fread(&m_UserConfig,sizeof(m_UserConfig),1,fd);
  pthread_mutex_unlock(&userCfg_mut);
  fclose(fd);
  return 0;
  */
}
USER_CONFIG_DATA UserCfg_Get()
{
  return m_UserConfig;
}
int UserCfg_Set(USER_CONFIG_DATA cfg)
{
  pthread_mutex_lock(&userCfg_mut);
  m_UserConfig = cfg;
  pthread_mutex_unlock(&userCfg_mut);
  return 0;
}
int InitConfig()
{
  return UserCfg_Init();
}
